package com.sscw.task.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guaike.core.mybatis.plugins.BaseDAO;
import com.sscw.task.enums.BattleStatusEnum;
import com.sscw.task.entity.MRecordBattlePushCoinsPO;
import com.sscw.task.mapper.MRecordBattlePushCoinsMapper;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;

@Repository
public class MRecordBattlePushCoinsDAO extends BaseDAO<MRecordBattlePushCoinsMapper, MRecordBattlePushCoinsPO> {

    public MRecordBattlePushCoinsPO findOneByUserId(Long battleId, Long userId) {
        LambdaQueryWrapper<MRecordBattlePushCoinsPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordBattlePushCoinsPO::getBattleId, battleId);
        queryWrapper.eq(MRecordBattlePushCoinsPO::getUserId, userId);
        return getBaseMapper().selectOne(queryWrapper);
    }

    public MRecordBattlePushCoinsPO findLatestBattleByUserId(Long userId) {
        LambdaQueryWrapper<MRecordBattlePushCoinsPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordBattlePushCoinsPO::getBattleDate, LocalDate.now());
        queryWrapper.eq(MRecordBattlePushCoinsPO::getUserId, userId);
        queryWrapper.orderByDesc(MRecordBattlePushCoinsPO::getId);
        queryWrapper.last("limit 0,1");
        return getBaseMapper().selectOne(queryWrapper);
    }

    public MRecordBattlePushCoinsPO findBattlingByUserId(Long userId) {
        LambdaQueryWrapper<MRecordBattlePushCoinsPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordBattlePushCoinsPO::getBattleDate, LocalDate.now());
        queryWrapper.eq(MRecordBattlePushCoinsPO::getUserId, userId);
        queryWrapper.eq(MRecordBattlePushCoinsPO::getStatus, BattleStatusEnum.battling.getCode());
        return getBaseMapper().selectOne(queryWrapper);
    }

    public long countUserTodayBattle(Long userId) {
        LambdaQueryWrapper<MRecordBattlePushCoinsPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordBattlePushCoinsPO::getBattleDate, LocalDate.now());
        queryWrapper.eq(MRecordBattlePushCoinsPO::getUserId, userId);
        return getBaseMapper().selectCount(queryWrapper);
    }
}
